package com.hu.zhcc.shiro.service.xsf.impl;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.common.entity.PageRequest;
import com.hu.zhcc.shiro.dao.xsf.TVaccinNoteMapper;
import com.hu.zhcc.shiro.entity.lgx.TMember;
import com.hu.zhcc.shiro.entity.xsf.dto.TMealsDTO;
import com.hu.zhcc.shiro.entity.xsf.dto.TVaccinNoteDTO;
import com.hu.zhcc.shiro.entity.xsf.pojo.TMeals;
import com.hu.zhcc.shiro.entity.xsf.pojo.TVaccinNote;
import com.hu.zhcc.shiro.service.xsf.TVaccinNoteService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TVaccinNoteImpl implements TVaccinNoteService {
    @Autowired
    private TVaccinNoteMapper tVaccinNoteMapper;

    @Override
    public Page<TVaccinNoteDTO> queryAllTVaccinNote(Map<String, Object> paremeters, int offset, int limit) {
        int total = this.tVaccinNoteMapper.countTVaccinNote(paremeters);
        List<TVaccinNoteDTO> dtoList = null;
        if(total > 0) {
            PageRequest pageRequest = new PageRequest(offset, limit, paremeters, null);
            List<TVaccinNote> list = this.tVaccinNoteMapper.queryAllTVaccinNote(pageRequest);
            dtoList = new ArrayList<TVaccinNoteDTO>(list.size());
            for(TVaccinNote tVaccinNote : list) {
                TVaccinNoteDTO dto = new TVaccinNoteDTO();
                BeanUtils.copyProperties(tVaccinNote, dto);
                dtoList.add(dto);
            }
        }
        return new Page<TVaccinNoteDTO>(total, dtoList);
    }


    @Override
    public TVaccinNoteDTO getById(int id) {
        TVaccinNote tVaccinNote = this.tVaccinNoteMapper.selectByPrimaryKey(id);
        if(tVaccinNote == null) {
            return null;
        }
        TVaccinNoteDTO dto = new TVaccinNoteDTO();
        BeanUtils.copyProperties(tVaccinNote, dto);
        return dto;
    }

    @Override
    public TVaccinNoteDTO saveTVaccinNote(TVaccinNoteDTO dto) {
        TVaccinNote tMeals = new TVaccinNote();
        BeanUtils.copyProperties(dto, tMeals);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        tMeals.setVaccinDate(simpleDateFormat.format(new Date()));
        this.tVaccinNoteMapper.insertSelective(tMeals);
        dto.setId(tMeals.getId());
        return dto;
    }

    @Override
    public int removeTVaccinNote(int id) {
        return tVaccinNoteMapper.deleteByPrimaryKey(id);

    }

    @Override
    public int updateTVaccinNote(TVaccinNote tVaccinNote) {
        return tVaccinNoteMapper.updateByPrimaryKeySelective(tVaccinNote);

    }
}
